<?php

class Video {

    function selecionarTudo($admin) {
        $con = new mysqli();
        $con = Connection::getConnection();

        $stmt = $con->prepare("SELECT video_id, video_url FROM video ORDER BY video_data ASC");
        $stmt->execute();
        $stmt->store_result();
        $count = $stmt->num_rows;

        if ($count > 0) {
            $stmt->bind_result($video_id, $video_url);
            while ($stmt->fetch()) {
                echo '<div class="video">';
                echo '        <object width="200" height="180"><param name="movie" value="http://www.youtube.com/v/' . $video_url . '"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/' . $video_url . '" type="application/x-shockwave-flash" wmode="transparent" width="200" height="180"></embed></object>';
                if (isset($_SESSION['logado']) && $_SESSION['logado'] == 1 && $admin) {
                    echo '        <span class="botao-vermelho" onclick="excluir(' . $video_id . ')">excluir</span>';
                }
                echo '    </div>';
            }
        } else {
            echo 'Nenhum video cadastrado.';
        }

        $con->close();
    }

    function excluir() {
        $con = new mysqli();
        $con = Connection::getConnection();

        $video_id = $_POST['video_id'];
        if (empty($video_id)) {
            header("HTTP/1.0 400 ID inválido");
            return;
        }

        $stmt = $con->prepare("DELETE FROM video WHERE video_id = ?");
        $stmt->bind_param('i', $video_id);
        $stmt->execute();
        $count = $stmt->affected_rows;

        if ($count == 0) {
            header("HTTP/1.0 500 Falha ao deletar o registro. Contate o administrador.");
        }

        $con->close();
    }

    function adicionar() {
        $con = new mysqli();
        $con = Connection::getConnection();

        $video_url = $_POST['video_url'];
        if (empty($video_url)) {
            header("HTTP/1.0 400 A url do vídeo não pode estar em branco.");
            return;
        }

        $stmt = $con->prepare("INSERT INTO video (video_url, video_data) VALUES (?, CURDATE())");
        $stmt->bind_param('s', $video_url);

        $stmt->execute();
        $count = $stmt->affected_rows;

        if ($count == 0) {
            header("HTTP/1.0 500 Falha ao inserir o registro. Contate o administrador.");
        }

        $con->close();
    }

}

?>
